package com.scan.bus.util;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.Display;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.hello.scan.ScanCallBack;
import com.hello.scan.ScanTool;
import com.scan.bus.MainActivity;
import com.scan.bus.MyApp;
import com.scan.bus.bean.DeviceContentEntity;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

import cn.jpush.android.api.JPushInterface;

public class DeviceInfoUtil {

    /**
     * 判断使用模式
     * @return 返回true表示表示该机型已经适配，false表示该机型还没有适配
     */
    public static  boolean initScanTool(Context context, ScanCallBack scanCallBack) {
        for (String s : MyApp.sDevices.keySet()) {
            if (s.equals(Build.MODEL)) {
                Pair<String, Integer> pair = MyApp.sDevices.get(s);
                if (pair == null) continue;
                ScanTool.GET.initSerial(context, pair.first, pair.second, scanCallBack);
                return true;
            }
        }
        return false;
    }


    public static void getUniqueId(Context context) {
        try {
            String androidId = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            SharedPrefUtil.saveCacheSharedPrf("X-Device-ID", TextUtils.isEmpty(androidId) ? UUID.nameUUIDFromBytes(Build.SERIAL.getBytes()).toString() : androidId);
        } catch (Exception e) {
            SharedPrefUtil.saveCacheSharedPrf("X-Device-ID", UUID.nameUUIDFromBytes(Build.SERIAL.getBytes()).toString());
        }
    }


    public static String getGprsIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return null;
    }

    public static String getWifiIpAddress(Context context) {
        //获取wifi服务
        WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        //判断wifi是否开启
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo==null)
            return null;
        int ipAddress = wifiInfo.getIpAddress();
        return TransformUtil.intToIp(ipAddress);
    }

    /**
     * 网络已经连接，然后去判断是wifi连接还是GPRS连接
     * 设置一些自己的逻辑调用
     */
    public static String getDeviceIp(Context context) {
        ConnectivityManager manager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfoMobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfoMobile != null){
            NetworkInfo.State gprs = networkInfoMobile.getState();
            if (gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING) {
                return getGprsIpAddress();
            }
        }

        NetworkInfo networkInfoWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfoWifi != null){
            NetworkInfo.State wifi = networkInfoWifi.getState();
            //判断为wifi状态下才加载广告，如果是GPRS手机网络则不加载！
            if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
                return getWifiIpAddress(context);
            }
        }
        return null;
    }

    public static String getDeviceRatio(Activity activity) {
        Display mDisplay = activity.getWindowManager().getDefaultDisplay();
        String deviceRatio=String.valueOf(mDisplay.getWidth())+"x"+String.valueOf(mDisplay.getHeight());
        return deviceRatio;
    }

    /**
     * 屏幕宽
     * @param context
     * @return
     */
    public static int getDeviceWidth(Context context) {
        int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        return widthPixels;
    }

    /**
     * 屏幕高
     * @param context
     * @return
     */
    public static int getDeviceHeight(Context context) {
        int heightPixels = context.getResources().getDisplayMetrics().heightPixels;
        return heightPixels;
    }

//    public static String getDeviceId(Context context) {
//        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        return tm.getDeviceId();
//    }

    /**
     * 获取当前社保信息
     * @param deviceContent
     * @return
     */
    public static DeviceContentEntity getCurrentDeviceContent(List<DeviceContentEntity> deviceContent) {
        String dateTime = TimeUtil.getFormatTime(TimeUtil.FORMAT_HH_MM_ss);
        long nowTime = TimeUtil.getHHmmssLong(dateTime);
        for(DeviceContentEntity content:deviceContent){
            long start_time = TimeUtil.getHHmmssLong(content.start_time);
            long end_time = TimeUtil.getHHmmssLong(content.end_time);
            LogUtil.httpLogW(content.start_time+"  nowTime:"+nowTime +" start_time:"+start_time +" end_time:"+end_time);
            if(nowTime>=start_time&&nowTime<=end_time){
                return content;
            }
        }
        return new DeviceContentEntity();
    }

    @SuppressLint("NewApi")
    public static boolean checkDeviceHasNavigationBar(Activity activity) {
        WindowManager windowManager = activity.getWindowManager();
        Display d = windowManager.getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            d.getRealMetrics(realDisplayMetrics);
        }

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }
}
